#!/bin/bash

if [ -z "$1" ]
then
    echo "Usage: `basename $0` EBSU_DIR"
    exit 1
fi

if [ ! -d "$1" ]
then
    echo $1 is not a directory, but should be
    exit 1
fi

EFI_DIR=`find $1 -name efi -type d 2>/dev/null | head -n 1`
if [ $? -ne 0 ] || [ -z "$EFI_DIR" ]
then
    echo "`basename $0`: Can not find 'efi' directory, quit!"
    exit 1
fi


SPU=$EFI_DIR/configutils/cfg/spu.ini
PCI=$EFI_DIR/configutils/cfg/PciTable.ini

# Make temp list containing firmware version
cat $SPU | awk -F '=' 'BEGIN{ \
    is_first_line = 1; \
    print_this_key = 0; \
    is_nic_card = 0; \
    seperator=";"; \
    compcnt=0; \
    name_trans["rx6600"]="Sapphire"; \
    name_trans["rx3600"]="Ruby"; \
    name_trans["rx2660"]="Merlion"; \
    name_trans["bl860c"]="Tahiti"; \
    name_trans["bl870c"]="Bora"; \
    name_trans["rx7640"]="Mittlehorn"; \
    name_trans["rx8640"]="Kona"; \
    name_trans["hpvm"]="Virtual Machine"; \
    name_trans["sanddune"]="Sanddune"; \
    name_trans["SA5300"]="Smart Array 5300 A9825A/A9826A"; \
    name_trans["SA6402"]="Smart Array 6402 A9890A"; \
    name_trans["SA6404"]="Smart Array 6404 A9891A"; \
    name_trans["SAP600"]="Smart Array P600"; \
    name_trans["SAP400"]="Smart Array P400"; \
    name_trans["SAE500"]="Smart Array E500"; \
    name_trans["SAP700M"]="Smart Array P700M/512"; \
    name_trans["SAP800"]="Smart Array P800"; \
    name_trans["LP982"]="Emulex 982 A7298A"; \
    name_trans["LP9802"]="Emulex 9802 AB232A"; \
    name_trans["LP1050"]="Emulex 1050 AB467A/AB466A"; \
    name_trans["LP1150"]="Emulex FC2143 4GB PCI-X AD167A"; \
    name_trans["LPE1150"]="Emulex FC2243 4GB PCI-X AD168A"; \
    name_trans["LP11002"]="Emulex FC2142SR 4GB PCI-e A8002A"; \
    name_trans["LPE11002"]="Emulex FC2242SR 4GB PCI-e A8003A"; \
    name_trans["LPE1105"]="Emulex LPe1105 4GB Mezzanine"; \
    name_trans["LPE12000"]="Emulex LPe12000 8GB"; \
    name_trans["LPE12002"]="Emulex LPe12002 8GB Dual Port"; \
    name_trans["LPE1205"]="8Gb FC Mezz Emulex"; \
    name_trans["LSI1010"]="LSI U160"; \
    name_trans["LSI1030"]="LSI U320"; \
    name_trans["LSI1030_KMIX"]="LSI U320"; \
    name_trans["LSI1030_EMB"]="LSI U320"; \
    name_trans["LSI1030_HPVM"]="LSI U320"; \
    name_trans["LSI1068"]="LSI SAS 1068"; \
    name_trans["LSI1068_BL870C"]="LSI SAS 1068"; \
    name_trans["QLOGIC4G2460"]="QLogic FC1143 4GB PCI-X AB429A "; \
    name_trans["QLE4G2460"]="QLogic FC1142SR 4GB PCI-e AE311A"; \
    name_trans["QLOGIC4G2462"]="QLogic FC1243 4GB PCI-X AB379A/AB379B"; \
    name_trans["QLE4G2462"]="QLogic FC1242SR 4GB PCI-e AD300A"; \
    name_trans["QLE8G2560"]="QLogic QLE8G2560"; \
    name_trans["QLE8G2562"]="QLogic QLE8G2562"; \
    name_trans["QMH2562"]="8Gb FC Mezz Qlogic"; \
    name_trans["QLMEZZANINE"]="QLogic Mezzanine QMH2462 4GB"; \
    name_trans["NICCOPPER"]="Win/Linux 2port 1000Base-T Gigabit A9900A"; \
    name_trans["NICCASTOR"]="Intel Dual Copper NIC AB290A"; \
    name_trans["NICFIBER"]="Win/Linux 2port 1000Base-SX Gigabit A9899A"; \
    name_trans["ASHTACOPPER"]="HP PCIe 2-port 1000Base-T card AD337A/AD380A"; \
    name_trans["ASHTAFIBER"]="HP PCIe 2-port 1000Base-SX card AD338A/AD381A"; \
    name_trans["MOTHERNEFF"]="HP NC364m PCIe 4-Port Gigabit"; \
    name_trans["NICKRYPTON"]="HP NC532m Dual Port 10GbE BL-C Adapter"; \
    name_trans["CEDARHILL"]="HP NC360M PCIe 2-Port Gigabit "; \
    name_trans["10GIG"]="Win/Lin 10 Gigabit PCI-X Ethernet AD144A"; \
    name_trans["10GIGAD385A"]="HP PCI-X 266MHz 10GigE SR card AD385A"; \
    name_trans["NICHPVM"]="Windows Fast Ethernet Single Port 10/100 HPVM" \
}{\
    if ( $1 ~ /^\[.*\]$/ ) { \
        is_nic_card = 0;
        gsub( /\[|\]|" "/, "", $1 ); \
        lowname=tolower($1); \
        if ( lowname ~ /^platforms|^version/ ) { print_this_key = 0 } \
        else if ( lowname ~ /^rx.+|^bl.+|^hpvm|^sanddune/ ) {\
            if ( lowname ~ /.+_sys$/ ) { \
                gsub( /_SYS$/, "", $1 ); \
                $1 = tolower($1); \
                print_this_key = 1 \
            } else { print_this_key = 0 } \
        } else { \
            print_this_key = 1 ;\
            if ( lowname ~ /^nic.+|^ashta.+|^10gig.*/ ) { is_nic_card = 1 } \
        }\
        if ( print_this_key ) {\
            tmp = sprintf("%s:", name_trans[$1]); \
            name = sprintf("    - %-50s", tmp); \
            if ( is_first_line ) { printf("%s" ,name); is_first_line = 0 }\
            else { printf("\n%s" ,name) } \
        }\
    }\
    compname = "";\
    if ( $1 ~ /^component_count/ ) { compcnt = $2 } \
    if ( $1 ~ /^component_1/ ) { \
        split( $2, arr, "," ); \
        if       ( arr[1] ~ /^sfw_.+/ ) { name_1="sfw"; name_2="mp "; name_3="" } \
        else if  ( arr[1] ~ /^sfw/ )    { name_1="sfw"; name_2="bmc"; name_3="mp " }\
        else                            { name_1="fw "; name_2="drv"; name_3="" }\
        compname = name_1;\
    }\
    if ( $1 ~ /^component_2/ ) { compname = name_2 }\
    if ( $1 ~ /^component_3/ ) { compname = name_3 }\
    if ( compname != "" ) {\
        split( $2, arr, "," ); \
        value = sprintf( "%s%s", arr[2], seperator); \
        if ( is_nic_card ) {\
            printf("%-4s%-12s", name_1, "-;"); \
            printf("%-4s%-12s", name_2, value); \
        }else{\
            printf("%-4s%-12s", compname, value); \
            if ( compcnt == 1 ) {\
                printf("%-4s%-12s", name_2, "-;") \
            }\
        }\
    }\
}END{\
    printf("\n")\
}' | awk -F ':' 'BEGIN{ arr["nothing"] = "1" }{\
    name = $1; \
    if ( ! ( name in arr ) ) {\
        arr[name] = "1"; \
        print $0; \
    }\
}'> firmware_version.txt

unix2dos firmware_version.txt


exit 0

# We don't needs the following steps since we hard coded component names

# Get component name through PciTable.ini
rm -rf ./tmp_link.lst
let cnt=1
cat firmware_version.txt | awk -F ';' '{print $2}' | while read keyname
do
    cat $PCI | awk -F '=' 'BEGIN{ key="'"$keyname"'"; item_start=0 }{\
        if ( $1 ~ key ) { item_start=1 }\
        if ( ( item_start == 1 ) && ( $1 ~ /^DESCRIPTION/ ) ) {\
            printf("%s;", $2 );\
            exit 0\
        }\
    }' >> ./tmp_link.lst
    head -n $cnt firmware_version.txt | tail -n 1 | awk -F ';' '{ for (i=1; i<NF; i++) { printf("%s;", $i) } printf("\n") }' >> ./tmp_link.lst
    let cnt=cnt+1
done



# Format output
cat $TMPCPN | awk -F ';' 'BEGIN{ }{\
    name = $1; \
    if ( name ~ /^System Firmware/ ) { name = $2; arr["0"]="1"}\
    if ( ! ( name in arr ) ) {\
        arr[name] = "1"; \
        printf("    - %-53s  ", name); \
        for (i=4; i<NF-1; i++) { ver=sprintf("%s;", $i); printf("%-16s", ver) } \
        printf("%-16s\n", $i); \
    }\
}' > firmware_version.txt

#rm -rf $TMPFWV $TMPCPN
unix2dos firmware_version.txt
